package org.ruoyi.interview.controller;

import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.domain.model.LoginUser;
import org.ruoyi.common.satoken.utils.LoginHelper;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteCategoryVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.bo.InterviewQuestionFavoriteBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteVo;
import org.ruoyi.interview.service.IInterviewQuestionFavoriteService;

import java.util.Arrays;
import java.util.List;

/**
 * 面试题收藏控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/interview/favorite")
public class InterviewQuestionFavoriteController extends BaseController {

    private final IInterviewQuestionFavoriteService interviewQuestionFavoriteService;

    /**
     * 查询面试题收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo<InterviewQuestionFavoriteCategoryVo> list(InterviewQuestionFavoriteBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return interviewQuestionFavoriteService.selectInterviewQuestionFavoriteList(bo, pageQuery);
    }

    /**
     * 查询面试题收藏列表及详情
     */
    @GetMapping("/listFavoriteQuestion")
    public TableDataInfo<InterviewQuestionFavoriteCategoryVo> listFavoriteQuestion(InterviewQuestionFavoriteBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return interviewQuestionFavoriteService.selectInterviewQuestionFavoriteQuestionList(bo, pageQuery);
    }


    /**
     * 获取面试题收藏详细信息
     *
     * @param favoriteId 收藏主键
     */
    @GetMapping("/{favoriteId}")
    public R<InterviewQuestionFavoriteVo> getInfo(@PathVariable("favoriteId") Long favoriteId) {
        return R.ok(interviewQuestionFavoriteService.selectInterviewQuestionFavoriteById(favoriteId));
    }

    /**
     * 新增面试题收藏
     */
    @Log(title = "面试题收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InterviewQuestionFavoriteBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUserId(loginUser.getUserId());
        return toAjax(interviewQuestionFavoriteService.insertInterviewQuestionFavorite(bo) ? 1 : 0);
    }

    /**
     * 取消面试题收藏
     */
    @Log(title = "面试题收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{favoriteId}")
    public R<Void> remove(@PathVariable Long favoriteId) {
        return toAjax(interviewQuestionFavoriteService.deleteInterviewQuestionFavoriteByQuestionId(favoriteId));
    }

    /**
     * 检查面试题是否已收藏
     */
    @GetMapping("/check")
    public R<InterviewQuestionFavoriteVo> checkFavorite(@RequestParam Long userId, @RequestParam Long questionId) {
        InterviewQuestionFavoriteVo favorite = interviewQuestionFavoriteService.selectInterviewQuestionFavoriteByUserIdAndQuestionId(userId, questionId);
        return R.ok(favorite);
    }
}
